import plugin from '../../../lib/plugins/plugin.js';
import common from '../../../lib/common/common.js';
import fetch from 'node-fetch';

let gs_speakermap = "空,荧,派蒙,纳西妲,阿贝多,温迪,枫原万叶,钟离,荒泷一斗,八重神子,艾尔海森,提纳里,迪希雅,卡维,宵宫,莱依拉,赛诺,诺艾尔,托马,凝光,莫娜,北斗,神里绫华,雷电将军,芭芭拉,鹿野院平藏,五郎,迪奥娜,凯亚,安柏,班尼特,琴,柯莱,夜兰,妮露,辛焱,珐露珊,魈,香菱,达达利亚,砂糖,早柚,云堇,刻晴,丽莎,迪卢克,烟绯,重云,珊瑚宫心海,胡桃,可莉,流浪者,久岐忍,神里绫人,甘雨,戴因斯雷布,优菈,菲谢尔,行秋,白术,九条裟罗,雷泽,申鹤,迪娜泽黛,凯瑟琳,多莉,坎蒂丝,萍姥姥,罗莎莉亚,留云借风真君,绮良良,瑶瑶,七七,奥兹,米卡,夏洛蒂,埃洛伊,博士,女士,大慈树王";
let sr_speakermap = "三月七,娜塔莎,希露瓦,虎克,克拉拉,丹恒,希儿,布洛妮娅,瓦尔特,杰帕德,佩拉,姬子,艾丝妲,白露,星,穹,桑博,伦纳德,停云,罗刹,卡芙卡,彦卿,史瓦罗,螺丝咕姆,阿兰,银狼,素裳,丹枢,黑塔,景元,帕姆,可可利亚,半夏,符玄,公输师傅,奥列格,青雀,大毫,青镞,费斯曼,绿芙蓉,镜流,信使";
let bh3_speakermap = "丽塔,失落迷迭,缭乱星棘,伊甸,伏特加女孩,狂热蓝调,莉莉娅,萝莎莉娅,八重樱,八重霞,卡莲,第六夜想曲,卡萝尔,姬子,极地战刃,布洛妮娅,次生银翼,理之律者,真理之律者,迷城骇兔,希儿,魇夜星渊,黑希儿,帕朵菲莉丝,天元骑英,幽兰黛尔,德丽莎,月下初拥,朔夜观星,暮光骑士,明日香,李素裳,格蕾修,梅比乌斯,渡鸦,人之律者,爱莉希雅,爱衣,天穹游侠,琪亚娜,空之律者,终焉之律者,薪炎之律者,云墨丹心,符华,识之律者,维尔薇,始源之律者,雷电芽衣,雷之律者,苏莎娜,阿波尼亚";
let wd_speakermap = "陆景和,莫弈,夏彦,左然";

export class voicecreate extends plugin {
	constructor() {
		super({
			name: '[Xs插件]米游语音生成',
			dsc: '米游语音生成',
			event: 'message',
			priority: -500,
			rule: [
				{
				reg: `^#(语音|合成)帮助$`,
				fnc: `voicecrehelp`
				},
				{
				reg: `^#?(.*)say(.*)$`,
				fnc: 'voicesend'
				}
			]
		})
	}

	async voicesend(e) {
	  let speaker = e.msg.replace(/say(.*)$/g, '') .replace(/#/g, '');
	  let test = await this.detectspeak(speaker);
	  if (test != 1) {
		e.reply(`未知的说话角色，发送"#语音帮助"查看列表`,true);
		return true;
	  }
      if (/(.*)say/.test(e.msg)) {
        e.msg = e.msg.replace(/(.*)say/g, '');
        let msg = e.msg;
        if (!msg) {
			e.reply(`未知的说话内容，发送"#语音帮助"查看列表`,true);
			return true;
		} else {
		let geturl = `https://api.lolimi.cn/API/yyhc/y.php?msg=${msg}&speaker=${speaker}`;
		let res = await fetch(geturl);
		res = await res.json();
		if (!res.music) {
			e.reply(`返回值错误(可能是api问题)，若确保您的参数无错，然后请联系插件作者反馈`);
			return true;
		}
		e.reply(`语音合成成功\n合成角色:${speaker}\n合成文本:${msg}\n正在发送语音,请稍后...:`,true);
		let music = res.music
		let result = await uploadRecord(music,0,false);
		e.reply(result);
		return true;
        }
      }
	}

	async voicecrehelp(e) {
		let msg = `"角色名"say"xxx"`
		let tip = `xxx代表任意内容\n` +
			`\n使用示例：\n` +
			`派蒙say你好\n` +
			`\n注：不支持别名，请输入准确的名字`
		let gs_speakersort = gs_speakermap.split(",");
		let gs_speakertip = "原神支持角色列表：\n";
		for (let i = 0; i < gs_speakersort.length; i++) {
			gs_speakertip += gs_speakersort[i];
			if (i % 2 == 0) gs_speakertip += "　　";
			else gs_speakertip += "\n";
		}
		let sr_speakersort = sr_speakermap.split(",");
		let sr_speakertip = "崩坏·星穹铁道支持角色列表：\n";
		for (let i = 0; i < sr_speakersort.length; i++) {
			sr_speakertip += sr_speakersort[i];
			if (i % 2 == 0) sr_speakertip += "　　";
			else sr_speakertip += "\n";
		}
		let bh3_speakersort = bh3_speakermap.split(",");
		let bh3_speakertip = "崩坏三支持角色列表：\n";
		for (let i = 0; i < bh3_speakersort.length; i++) {
			bh3_speakertip += bh3_speakersort[i];
			if (i % 2 == 0) bh3_speakertip += "　　";
			else bh3_speakertip += "\n";
		}
		let wd_speakersort = wd_speakermap.split(",");
		let wd_speakertip = "未定事件簿支持角色列表：\n";
		for (let i = 0; i < wd_speakersort.length; i++) {
			wd_speakertip += wd_speakersort[i];
			if (i % 2 == 0) wd_speakertip += "　　";
			else wd_speakertip += "\n";
		}
		let msgx = await common.makeForwardMsg(e, [msg, tip, gs_speakertip, sr_speakertip, bh3_speakertip, wd_speakertip], `语音生成帮助`);
		e.reply(msgx);
		return true;
	}

	async detectspeak(speakername) {
	  let gsspeakersort = gs_speakermap.split(",");
      let srspeakersort = sr_speakermap.split(",");
      let bh3speakersort = bh3_speakermap.split(",");
      let wdspeakersort = wd_speakermap.split(",");
		for (let i = 0; i < gsspeakersort.length; i++) {
			if (gsspeakersort[i] == speakername) return 1;
		}
		for (let i = 0; i < srspeakersort.length; i++) {
			if (srspeakersort[i] == speakername) return 1;
		}
		for (let i = 0; i < bh3speakersort.length; i++) {
			if (bh3speakersort[i] == speakername) return 1;
		}
		for (let i = 0; i < wdspeakersort.length; i++) {
			if (wdspeakersort[i] == speakername) return 1;
		}
		return 0;
	}
}